package com.sihai.ehr.service.impl;

import com.sihai.core.*;
import com.sihai.ehr.model.biz.SalaryIndividualTax;
import com.sihai.ehr.model.biz.SalaryIndividualTaxRadix;
import com.sihai.ehr.model.biz.SalaryIndividualTaxRule;
import com.sihai.ehr.service.IndividualTaxService;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 个人所税实现类
 *
 * @author zhang
 */
@Service("individualTaxService")
public class IndividualTaxServiceImpl implements IndividualTaxService {

    @Resource
    private DataAccessor dataAccessor;

    @Override
    public LimitedList<SalaryIndividualTax> query(Long orgId, Limit limit, Sorter sorter) {
        return dataAccessor.findByHql(SalaryIndividualTax.class, "from SalaryIndividualTax where organization.id=?", new Object[]{orgId}, limit, sorter);
    }

    @Override
    public void save(SalaryIndividualTax salaryIndividualTax) {
        if (salaryIndividualTax == null)
            return;
        dataAccessor.save(salaryIndividualTax);
    }

    @Override
    public SalaryIndividualTax get(Long id) {
        if (id == null)
            return null;
        return dataAccessor.get(SalaryIndividualTax.class, id);
    }

    @Override
    public SalaryIndividualTaxRule getRule(Long id) {
        if (id == null)
            return null;
        return dataAccessor.get(SalaryIndividualTaxRule.class, id);
    }

    @Override
    public void remove(final String taxIds) {
        if (taxIds == null || taxIds.isEmpty()) {
            return;
        }

        dataAccessor.batchUpdate(new QueryCreator() {
            @Override
            public Query createQuery(Session session) {
                return session.createQuery(String.format("delete from SalaryIndividualTax where id in (%s)", taxIds));
            }
        });
    }

    @Override
    public LimitedList<SalaryIndividualTaxRule> queryTaxRule(Long orgId, Limit limit, Sorter sorter) {
        String hql = "from SalaryIndividualTaxRule r where r.organization.id=?";
        return dataAccessor.findByHql(SalaryIndividualTaxRule.class, hql, new Object[]{orgId}, limit, sorter);
    }

    @Override
    public void saveRule(SalaryIndividualTaxRule salaryIndividualTaxRule) {
        if (salaryIndividualTaxRule == null)
            return;
        dataAccessor.save(salaryIndividualTaxRule);
    }

    @Override
    public void removeRule(final String taxIds) {
        if (taxIds == null || taxIds.isEmpty()) {
            return;
        }

        dataAccessor.batchUpdate(new QueryCreator() {
            @Override
            public Query createQuery(Session session) {
                return session.createQuery(String.format("delete from SalaryIndividualTaxRule where id in (%s)", taxIds));
            }
        });
    }

    @Override
    public SalaryIndividualTaxRadix getRadix(final Long organizationId) {
        return dataAccessor.findUnique(SalaryIndividualTaxRadix.class, new QueryCreator() {
            @Override
            public Query createQuery(Session session) {
                String hql = "from SalaryIndividualTaxRadix where organization.id=?";
                Query query = session.createQuery(hql);
                query.setLong(0,organizationId);
                return query;
            }
        });
    }

    @Override
    public void saveRadix(SalaryIndividualTaxRadix salaryIndividualTaxRadix) {
        if(salaryIndividualTaxRadix==null)
            return;
        dataAccessor.save(salaryIndividualTaxRadix);
    }
}
